package stringFun;

public class stringDemo2 {
    public static void main(String[] args) {
        String str = "hello world";
        //System.out.println(str.length());//11
        /*for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != ' '){
                System.out.println(str.charAt(i));
            }
        }*/
        int count = strCount('l',str);
        System.out.println(count);
        String result = strReverse(str);
        System.out.println(result);
        String res = priceFormat( 436792);
        System.out.println(res);
    }

    public static int strCount(char key,String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == key){
                count++;
            }
        }
        return count;
    }

    public static String strReverse(String str) {
        StringBuilder result = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) {
            result.append(str.charAt(i));
        }
        return result.toString();
    }

    public static String priceFormat(int price) {
        String [] formatNumber = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //String [] formatUnit = {"拾","佰","仟","万","亿"};
        String[] formatUnit = {"亿", "仟", "佰", "拾","万","仟", "佰", "拾","元"};
        String str = String.valueOf(price);
        //如果 str 长度小于 9，需要在前面补 0
        if(str.length()<9){
            str = "0".repeat(9-str.length())+str;
        }
        //stringBuilder 是可变字符串，效率高
        StringBuilder result = new StringBuilder();
        /*
         * for (int i = 0; i < str.length(); i++) {
            int num = str.charAt(i) - '0';
            result.append(formatNumber[num]);
            if(i==0){
                result.append(formatUnit[4]);
            }else if(i==1 || i==5){
                result.append(formatUnit[2]);
            }else if(i==2 || i==6){
                result.append(formatUnit[1]);
            }else if(i==3 || i==7){
                result.append(formatUnit[0]);
            } else if (i == 4) {
                result.append(formatUnit[3]);
            }
        }
        */
        for (int i = 0; i < str.length(); i++) {
            int num = str.charAt(i) - '0';
            result.append(formatNumber[num]);
            // 根据索引从单位数组中获取对应的单位并添加
            result.append(formatUnit[i]);
        }
        //System.out.println(result.reverse());
        return result.toString();
    }
}
